package com.scenery;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.scenery.services.SetManager;
import com.scenery.services.UserServices;
import com.scenery.util.HttpUtil;
import com.scenery.util.Loger;
import com.scenery.view.UserListView;
import com.scenery.vo.User;
/****
 * 用户列表
 * **/
public class UserFriendActivity extends Activity {
	String tag=SelectActivity.class.getName();
	UserListView userListView;
	ArrayList<User> userList;
	Button btnMapUser;
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO Put your code here
		setContentView(R.layout.user_list);
		userListView=(UserListView) findViewById(R.id.userListView);
		btnMapUser=(Button)findViewById(R.id.btnMapUser);
		btnMapUser.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(userList.size()==0)
				{
					Toast.makeText(UserFriendActivity.this, "记录为空", Toast.LENGTH_LONG).show();
				}else  if(userList.size()>0)
				{
					Intent it=new Intent();
					Bundle bundle = new Bundle();
					bundle.putParcelableArrayList("list", userList);
					it.putExtras(bundle);
					it.setClass(UserFriendActivity.this, UserFriendMapActivity.class);
					startActivity(it);
				}
				
			}
		});
		if(!HttpUtil.isNetWorkConnected(UserFriendActivity.this))
		{
			Toast.makeText(UserFriendActivity.this, "网络不通,请先打开网络！", Toast.LENGTH_LONG).show();
		}else {
			new UserListAsyncTask().execute(0);
		}
	}
	class UserListAsyncTask extends AsyncTask<Integer,Integer,Integer>{
		int type;//
		ProgressDialog dialog=new ProgressDialog(UserFriendActivity.this);
		
		
		@Override
		protected Integer doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			//return UserServices.login(user, pwd);
			
			//cityList=new TcSceneryServices().getCityListByProvinceId();
			SetManager set=new SetManager(UserFriendActivity.this);
			UserServices services=new UserServices();
			services.setIp(set.getIp());
			services.setWebport(Integer.parseInt(set.getWebServerport()));
			
			userList=services.friend(set.getUserId());
			
			Loger.d(tag, userList.toArray().toString());
			return 0;
		}
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog.setMessage("加载数据中");
			dialog.show();
		}
		@Override
		protected void onPostExecute(Integer result) 
		{
			
			Log.d(tag, "onPostExecute:"+result);
			dialog.dismiss();
			
			if(userList.size()==0)
			{
				Toast.makeText(UserFriendActivity.this, "记录为空", Toast.LENGTH_LONG).show();
			}else  if(userList.size()>0)
			{
				Toast.makeText(UserFriendActivity.this, "找到"+userList.size()+"好友", Toast.LENGTH_LONG).show();
				userListView.refresh(userList);
			}
			
//			switch(result)
//			{
//				case 0:
//					Toast.makeText(SelectActivity.this, "成功", Toast.LENGTH_LONG).show();
//					
//					break;
//				case 1:
//					Toast.makeText(SelectActivity.this, "连接不到服务器", Toast.LENGTH_LONG).show();
//					break;
//				case 2:
//					Toast.makeText(SelectActivity.this, "异常", Toast.LENGTH_LONG).show();
//					break;
//			}
		}
		
	};
}
